<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="hAdd('0',1)">添加权限</el-button>
        </div>
        <el-table border :data="permissons" row-key="id" default-expand-all>
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button v-if="row.type === 1" type="text" @click="hAdd(row.id,2)">添加</el-button>
              <el-button type="text" @click="hChange(row.id)">编辑</el-button>
              <el-button type="text">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <el-dialog
        v-if="showDialog"
        :title="isEdit ? '编辑' : '添加' "
        :is-edit="false"
        :visible.sync="showDialog"
        width="40%"
      >
        <permissionsDialog
          :list="list"
          :is-edit="isEdit"
          :q="q"
          @success="hSuccess"
          @closed="showDialog=false"
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getPermissionList } from '@/api/permisson'
import { tranListToTreeData } from '@/utils'
import permissionsDialog from '@/views/permissions/permissionsDialog.vue'
export default {
  components: {
    permissionsDialog
  },
  data() {
    return {
      isEdit: false,
      showDialog: false,
      permissons: [],
      q: {},
      list: []
    }
  },
  created() {
    this.loadInfo()
  },
  methods: {
    // 获取列表信息
    async loadInfo() {
      const res = await getPermissionList()
      // console.log(res)
      this.list = res.data.map(({ id, name, pid, code }) => ({ id, name, pid, code }))
      console.log(this.list)
      this.permissons = tranListToTreeData(res.data)
    },
    // 添加
    hAdd(pid, type) {
      console.log(pid, type)
      this.showDialog = true
      this.isEdit = false
      this.q.pid = pid
      this.q.type = type
    },
    // 编辑
    hChange(pid, type) {
      // console.log(pid, type)
      this.showDialog = true
      this.isEdit = true
      this.q.pid = pid
      this.q.type = type
    },
    // 子传父 关闭 dialog 渲染页面
    hSuccess() {
      this.showDialog = false
      this.loadInfo()
    }

  }
}
</script>
